package com.oba.base.service;

import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import com.oba.base.service.AbstractBean;
import com.oba.base.service.BaseService;
import com.oba.base.interfaces.IBaseBo;

public abstract class BaseBean<Tbo extends IBaseBo, TService extends BaseService> extends AbstractBean {
	private TService service;
	static public String LIST_PAGE_NAVIGATION = "list";
	static public String SINGLE_PAGE_NAVIGATION = "single";
	public static final String PAGE_PATH = "pagepath"; 
	private Boolean editing;
	private String path;
	

	protected String getPath() {
		return path;
	}

	public void goTarget(String path) {
		getSession().setAttribute(PAGE_PATH, path);
		this.path = path;
	}

	protected TService getService() {
		return service;
	}

	public void setService(TService service) {
		this.service = service;
	}

	/**
	 * 当前对象
	 */

	private Tbo curBo;

	public Tbo getCurBo() {
		return curBo;
	}

	public String doAddNew() {
		curBo = (Tbo) getService().getNewBo();
		setEditing(Boolean.TRUE);
		return SINGLE_PAGE_NAVIGATION;
	}

	public Boolean isEditing() {
		return editing;
	}

	private void setEditing(Boolean editing) {
		this.editing = editing;
	}

	public String doSaveContinue() {
		getService().save(curBo);
		return doAddNew();
	}

	public String doBack() {
		return doFind();
	}

	public String doView() {
		setEditing(Boolean.FALSE);
		load();
		return SINGLE_PAGE_NAVIGATION;
	}

	public String doEdit() {
		setEditing(Boolean.TRUE);
		load();
		return SINGLE_PAGE_NAVIGATION;
	}

	private void load() {

	}

	public abstract String doFind();

	/**
	 * 进入Bean对应页面时的初始化方法
	 */
	public abstract void init();

	public void setCurBo(Tbo curBo) {
		this.curBo = curBo;
	}

	public String getParam(String key) {
		FacesContext context = FacesContext.getCurrentInstance();
		Map<String, String> map = context.getExternalContext().getRequestParameterMap();
		return map.get(key);
	}
	
	public HttpSession getSession() {
		FacesContext ctx = FacesContext.getCurrentInstance();
		return (HttpSession) ctx.getExternalContext().getSession(false);
	}
}